﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.DAL;
using OneMile.Entity.Model;
using OneMile.Common;

namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MS0002", Name = "商家获取今日订单列表")]
    public class SGetTodayOrderList : BaseAPI,IProcessMessage
    {

        private DALSOrder dalShopOrder;
        protected DALSOrder DalShopOrder
        {
            get
            {
                if (dalShopOrder == null)
                {
                    dalShopOrder = DALFactory.getInstance().CreateSOrderDAL();
                }
                return dalShopOrder;
            }
        }

        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string shopId = reqMsg.Body.GetFieldValue("ShopId");
                if(String.IsNullOrEmpty(shopId))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }
                Guid guid = new Guid(shopId);
                List<EOrder> list = (List<EOrder>)DalShopOrder.GetTodayOrderList(guid);
                if (list == null||list.Count==0)
                {
                    rspMsg.Head.RspCode = MessageCode.Result_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Result_Empty.Msg;
                    return rspMsg;
                }
                var fl = rspMsg.Body.AddFieldList("Orders");
                for (int i = 0; i < list.Count; i++)
                {
                    var fll = fl.AddFieldList("Order");
                    fll.AddField("OrderId",list[i].OrderId.TString());
                    fll.AddField("OrderNumber", list[i].OrderNumber.TString());
                    fll.AddField("OrderName", list[i].OrderName);
                    fll.AddField("OrderAddress", list[i].OrderAddress);
                    fll.AddField("OrderMobile", list[i].OrderMobile);
                    fll.AddField("OrderType", list[i].OrderType.TString());
                    fll.AddField("UserId",list[i].UserId.TString());
                    fll.AddField("TotalPrice", list[i].TotalPrice.ToString("F2"));
                    fll.AddField("EarliestTime", list[i].EarliestTime.ToString("yyyy/MM/dd HH:mm:ss"));
                    fll.AddField("LatestTime", list[i].LatestTime.ToString("yyyy/MM/dd HH:mm:ss"));
                }


                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("ShopGetTodayOrderList" + e.Message);
                throw e;
            }
        }

        #endregion
    }
}